/**
 * Copyrights Christophe Cornu 2010, All rights reserved.
 */
package com.cctl.speaker.match;

import com.cctl.speaker.Sentence;
import com.cctl.speaker.Speaker;
import com.cctl.speaker.Speaker.Result;

/**
 * favors sentences whose words match the user words (in any order)
 * Take into account the number of letters in word matches
 * 
 * pb: doesn't match when accents missing
 * pb: doesn't value when order is correct
 */
public class Match2 implements Match {

	Sentence userInput;
	
	public void setUserInput(Sentence userInput) {
		this.userInput = userInput;
	}
	
	public Result calculateCorrelation(Sentence sentence) {
		if (sentence.words.length < userInput.words.length)
			return null;
		float n = 0, m = 0;
		for (String word : userInput.words) {
			if (Speaker.indexOf(word, sentence.words, false) != -1)
				n += word.length();
		}
		for (String word : sentence.words) {
			if (Speaker.indexOf(word, userInput.words, false) != -1)
				m += word.length();
		}
		if (n == 0 || m == 0)
			return null;
		Result result = new Result();
		result.score = n * m / (userInput.countLetters() * sentence.countLetters());
		result.sentence = sentence;
		return result;
	}
}
